<template>
  <xn-form-container
    :title="formData.ID ? '编辑产品型号全部信息' : '新增产品型号'"
    :width="550"
    :visible="visible"
    :destroy-on-close="true"
    @close="onClose"
  >
    <a-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      :label-col="{
        style: {
          width: '180px',
        },
      }"
      :wrapper-col="{ span: 12 }"
    >
      <a-form-item label="产品型号名称" name="PMName">
        <a-input
          v-model:value="formData.PMName"
          placeholder="请输入产品型号名称"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="产品型号编号" name="PMNo">
        <a-input
          v-model:value="formData.PMNo"
          placeholder="请输入产品型号编号"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="产品类型" name="PTID">
        <a-select
          v-model:value="formData.PTID"
          style="width: 100%"
          placeholder="请选择产品类型"
          :options="props.productType"
        ></a-select>
      </a-form-item>
      <a-form-item label="产品型号说明" name="PMDesp">
        <a-input
          v-model:value="formData.PMDesp"
          placeholder="请输入产品型号说明"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="老化时长(s)" name="AgeTimeLen">
        <a-input
          v-model:value="formData.AgeTimeLen"
          placeholder="请输入老化时长(s)"
          allow-clear
        />
      </a-form-item>
    </a-form>
    <!-- 人工检测参数 -->
    <a-form
      ref="testFormRef"
      :model="testCfgData"
      :rules="testDataRules"
      :label-col="{
        style: {
          width: '180px',
        },
      }"
      :wrapper-col="{ span: 12 }"
    >
      <a-form-item label="充电电压(V)最小值" name="VolMin">
        <a-input
          v-model:value="testCfgData.VolMin"
          placeholder="请输入充电电压最小值"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="充电电压(V)最大值" name="VolMax">
        <a-input
          v-model:value="testCfgData.VolMax"
          placeholder="请输入充电电压最小值"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="充电电流(A)最小值" name="CurMin">
        <a-input
          v-model:value="testCfgData.CurMin"
          placeholder="请输入充电电压最小值"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="充电电流(A)最大值" name="CurMax">
        <a-input
          v-model:value="testCfgData.CurMax"
          placeholder="请输入充电电压最大值"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="CC电阻基准(Ω)" name="CCR">
        <a-input
          v-model:value="testCfgData.CCR"
          placeholder="请输入CC电阻基准"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="CC电阻误差(%)" name="CCRError">
        <a-input
          v-model:value="testCfgData.CCRError"
          placeholder="请输入CC电阻误差"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="充电桩额定电流(A)" name="RatedCur">
        <a-input
          v-model:value="testCfgData.RatedCur"
          placeholder="请输入充电桩额定电流"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="CP峰值电压(V)" name="CPRealVol">
        <a-input
          v-model:value="testCfgData.CPRealVol"
          placeholder="请输入CP峰值电压"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="CP峰值电压误差(V)" name="CPRealVolError">
        <a-input
          v-model:value="testCfgData.CPRealVolError"
          placeholder="请输入CP峰值电压误差"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="CP频率最小值(Hz)" name="CPHzMin">
        <a-input
          v-model:value="testCfgData.CPHzMin"
          placeholder="请输入CP频率最小值"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="CP频率最大值(Hz)" name="CPHzMax">
        <a-input
          v-model:value="testCfgData.CPHzMax"
          placeholder="请输入CP频率最大值"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="占空比(%)" name="CPEmpty">
        <a-input
          v-model:value="testCfgData.CPEmpty"
          placeholder="请输入占空比"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="占空比误差" name="CPEmptyError">
        <a-input
          v-model:value="testCfgData.CPEmptyError"
          placeholder="请输入占空比误差"
          allow-clear
        />
      </a-form-item>
    </a-form>

    <!-- 自动检测参数 -->
    <a-form
      ref="autoTestFormRef"
      :model="autoTestData"
      :rules="autoTestDataRules"
      :label-col="{
        style: {
          width: '180px',
        },
      }"
      :wrapper-col="{ span: 12 }"
    >
      <a-form-item label="最大电压告警下限值(V)" name="MaxVolAlarmMin">
        <a-input
          v-model:value="autoTestData.MaxVolAlarmMin"
          placeholder="请输入最大电压告警下限值"
          allow-clear
        />
      </a-form-item>
      <a-form-item label=" 最大电压告警上限值(V)" name="MaxVolAlarmMax">
        <a-input
          v-model:value="autoTestData.MaxVolAlarmMax"
          placeholder="请输入最大电压告警上限值"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="最大电压合格下限值(V)" name="MaxVolPassMin">
        <a-input
          v-model:value="autoTestData.MaxVolPassMin"
          placeholder="最大电压合格下限值"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="最大电压合格上限值(V)" name="MaxVolPasMax">
        <a-input
          v-model:value="autoTestData.MaxVolPasMax"
          placeholder="请输入最大电压合格上限值"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="最小电压告警下限值(V)" name="MinVolAlarmMin">
        <a-input
          v-model:value="autoTestData.MinVolAlarmMin"
          placeholder="请输入最小电压告警下限值"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="最小电压告警上限值(V)" name="MinVolAlarmMax">
        <a-input
          v-model:value="autoTestData.MinVolAlarmMax"
          placeholder="请输入最小电压告警上限值"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="最小电压合格下限值(V)" name="MinVolPassMin">
        <a-input
          v-model:value="autoTestData.MinVolPassMin"
          placeholder="请输入最小电压合格下限值"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="最小电压合格上限值(V)" name="MinVolPassMax">
        <a-input
          v-model:value="autoTestData.MinVolPassMax"
          placeholder="请输入最小电压合格上限值"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="最大频率合格下限值(HZ)" name="MaxHzPassMin">
        <a-input
          v-model:value="autoTestData.MaxHzPassMin"
          placeholder="请输入最大频率合格下限值"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="最大频率合格上限值(Hz)" name="MaxHzPassMax">
        <a-input
          v-model:value="autoTestData.MaxHzPassMax"
          placeholder="请输入最大频率合格上限值"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="最大频率告警限值(Hz)" name="MaxHzAlarm">
        <a-input
          v-model:value="autoTestData.MaxHzAlarm"
          placeholder="请输入最大频率告警限值"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="最小频率合格下限值(HZ)" name="MinHzPassMin">
        <a-input
          v-model:value="autoTestData.MinHzPassMin"
          placeholder="请输入最小频率合格下限值"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="最小频率合格上限值(HZ)" name="MinHzPassMax">
        <a-input
          v-model:value="autoTestData.MinHzPassMax"
          placeholder="请输入最小频率合格上限值(HZ)"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="最小频率告警限值(HZ)" name="MinHzAlarm">
        <a-input
          v-model:value="autoTestData.MinHzAlarm"
          placeholder="请输入最小频率告警限值"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="最大CP合格下限值(%)" name="MaxCPPassMin">
        <a-input
          v-model:value="autoTestData.MaxCPPassMin"
          placeholder="请输入最大CP合格下限值"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="最大CP合格上限值(%)" name="MaxCPassMax">
        <a-input
          v-model:value="autoTestData.MaxCPassMax"
          placeholder="请输入最大CP合格上限值"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="最大CP告警值(%)" name="MaxCPAlarm">
        <a-input
          v-model:value="autoTestData.MaxCPAlarm"
          placeholder="请输入最大CP告警值"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="最小CP合格下限值(%)" name="MinCPPassMin">
        <a-input
          v-model:value="autoTestData.MinCPPassMin"
          placeholder="请输入最小CP合格下限值"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="最小CP合格上限值(%)" name="MinCPPassMax">
        <a-input
          v-model:value="autoTestData.MinCPPassMax"
          placeholder="请输入最小CP合格上限值"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="最小CP告警值(%)" name="MinCPAlarm">
        <a-input
          v-model:value="autoTestData.MinCPAlarm"
          placeholder="请输入最小CP告警值"
          allow-clear
        />
      </a-form-item>
    </a-form>

    <!-- 最后三个开关 -->
    <a-form
      ref="sitchFormRef"
      :rules="sitchFormRules"
      :model="formData"
      :label-col="{
        style: {
          width: '180px',
        },
      }"
      :wrapper-col="{ span: 12 }"
    >
      <a-form-item label="每托盘的产品数量" name="NumPerTuoPan">
        <a-input
          v-model:value="formData.NumPerTuoPan"
          placeholder="请输入每托盘的产品数量"
          allow-clear
        />
      </a-form-item>
      <a-form-item label="打包验证产品翻卡" name="IsCheckFanKa">
        <a-switch
          :checked="formData.IsCheckFanKa === 1"
          @click="changeFanka($event)"
        />
      </a-form-item>
      <a-form-item label="打包验证检测合格" name="IsCheckPass">
        <a-switch
          :checked="formData.IsCheckPass === 1"
          @click="changePassOf($event)"
        />
      </a-form-item>
      <a-form-item label="打包验证老化合格" name="IsCheckAgePass">
        <a-switch
          :checked="formData.IsCheckAgePass === 1"
          @click="changeAgePassOf($event)"
        />
      </a-form-item>
    </a-form>
    <template #footer>
      <a-button class="xn-mr8 mr10" @click="onClose">关闭</a-button>
      <a-button type="primary" @click="onSubmit">{{
        formData.ID ? "确认修改" : "确认新增"
      }}</a-button>
    </template>
  </xn-form-container>
</template>

<script setup>
import { required } from "@/utils/formRules";
import productmodelmgrApi from "@/api/productManagement/productmodelmgrApi";
import XnFormContainer from "@/components/XnFormContainer/index.vue";
import { message } from "ant-design-vue";
import { defineProps } from "vue";
const props = defineProps({
  productType: Array,
});
// 默认是关闭状态
const visible = ref(false);
const emit = defineEmits({ successful: null });
// 表单实例
const formRef = ref();
// 人工测试表单实例
const testFormRef = ref();
// 自动检测表单实例
const autoTestFormRef = ref();
// 开关表单实例
const sitchFormRef = ref();

// 表单数据
const formData = ref({});
// 表单中人工检测的配置
const testCfgData = ref({});
//表单中自动检测的配置
const autoTestData = ref({});
// 打开抽屉
const onOpen = (record) => {
  visible.value = true;
  if (record) {
    formData.value = Object.assign({}, record);
    testCfgData.value = JSON.parse(formData.value.TestCfg);
    autoTestData.value = JSON.parse(formData.value.AutoTestCfg);
  } else {
    formData.value = {};
    formData.value.IsCheckFanKa = 0;
    formData.value.IsCheckPass = 0;
    formData.value.IsCheckAgePass = 0;
    testCfgData.value = {};
    autoTestData.value = {};
  }
};
// 关闭抽屉
const onClose = () => {
  formRef.value.resetFields();
  visible.value = false;
};

// 默认要校验的
const formRules = {
  PMName: [required("请输入产品型号名称")],
  PMNo: [required("请输入产品型号编号")],
  PTID: [required("请输入产品类型ID")],

  AgeTimeLen: [required("请输入老化时长(s)")],
};

const testDataRules = {
  VolMin: [required("请输入充电电压最小值")],
  VolMax: [required("请输入充电电压最小值")],
  CurMin: [required("请输入充电电压最小值")],
  CurMax: [required("请输入充电电压最大值")],
  CCR: [required("请输入CC电阻基准")],
  CCRError: [required("请输入CC电阻误差")],
  RatedCur: [required("请输入充电桩额定电流")],
  CPRealVol: [required("请输入CP峰值电压")],
  CPRealVolError: [required("请输入CP峰值电压误差")],
  CPHzMin: [required("请输入CP频率最小值")],
  CPHzMax: [required("请输入CP频率最大值")],
  CPEmpty: [required("请输入占空比")],
  CPEmptyError: [required("请输入占空比误差")],
};
const autoTestDataRules = {
  MaxVolAlarmMin: [required("请输入最大电压告警下限值")],
  MaxVolAlarmMax: [required("请输入最大电压告警上限值")],
  MaxVolPassMin: [required("最大电压合格下限值")],
  MaxVolPasMax: [required("请输入最大电压合格上限值")],
  MinVolAlarmMin: [required("请输入最小电压告警下限值")],
  MinVolAlarmMax: [required("请输入最小电压告警上限值")],
  MinVolPassMin: [required("请输入最小电压合格下限值")],
  MinVolPassMax: [required("请输入最小电压合格上限值")],
  MaxHzPassMin: [required("请输入最大频率合格下限值")],
  MaxHzPassMax: [required("请输入最大频率合格上限值")],
  MaxHzAlarm: [required("请输入最大频率告警限值")],
  MinHzPassMin: [required("请输入最小频率合格下限值")],
  MinHzPassMax: [required("请输入最小频率合格上限值")],
  MinHzAlarm: [required("请输入最小频率告警限值")],
  MaxCPPassMin: [required("请输入最大CP合格下限值")],
  MaxCPassMax: [required("请输入最大CP合格上限值")],
  MaxCPAlarm: [required("请输入最大CP告警值")],
  MinCPPassMin: [required("请输入最小CP合格下限值")],
  MinCPPassMax: [required("请输入最小CP合格上限值")],
  MinCPAlarm: [required("请输入最小CP告警值")],
};
const sitchFormRules = {
  NumPerTuoPan: [required("请输入每托盘的产品数量")],
};

// 验证并提交数据
const onSubmit = () => {
  if (formData.value.ID) {
    Promise.all([
      formRef.value.validate(),
      testFormRef.value.validate(),
      autoTestFormRef.value.validate(),
      sitchFormRef.value.validate(),
    ]).then(() => {
      Object.keys(testCfgData.value).forEach((key) => {
        testCfgData.value[key] = parseFloat(testCfgData.value[key]);
      });
      Object.keys(autoTestData.value).forEach((key) => {
        autoTestData.value[key] = parseFloat(autoTestData.value[key]);
      });
      formData.value.TestCfg = JSON.stringify(testCfgData.value);
      formData.value.AutoTestCfg = JSON.stringify(autoTestData.value);

      productmodelmgrApi
        .modifyProductModel(formData.value)
        .then(() => {
          onClose();
          emit("successful");
        })
        .catch((err) => {
          console.log(err);
        });
    });
  } else {
    Promise.all([
      formRef.value.validate(),
      testFormRef.value.validate(),
      autoTestFormRef.value.validate(),
      sitchFormRef.value.validate(),
    ]).then(() => {
      Object.keys(testCfgData.value).forEach((key) => {
        testCfgData.value[key] = parseFloat(testCfgData.value[key]);
      });
      Object.keys(autoTestData.value).forEach((key) => {
        autoTestData.value[key] = parseFloat(autoTestData.value[key]);
      });
      formData.value.TestCfg = JSON.stringify(testCfgData.value);
      formData.value.AutoTestCfg = JSON.stringify(autoTestData.value);
      formData.value.PTID = parseInt(formData.value.PTID);
      formData.value.AgeTimeLen = parseInt(formData.value.AgeTimeLen);
      formData.value.NumPerTuoPan = parseInt(formData.value.NumPerTuoPan);
      productmodelmgrApi
        .addProductModel(formData.value)
        .then(() => {
          onClose();
          emit("successful");
          message.success("新增成功");
        })
        .catch((err) => {
          console.log(err);
        });
    });
  }
};

// 调用这个函数将子组件的一些数据和方法暴露出去
defineExpose({
  onOpen,
});
const changeFanka = (bool) => {
  let code = bool ? 1 : 0;
  formData.value.IsCheckFanKa = code;
};
const changePassOf = (bool) => {
  let code = bool ? 1 : 0;
  formData.value.IsCheckPass = code;
};

const changeAgePassOf = (bool) => {
  let code = bool ? 1 : 0;
  formData.value.IsCheckAgePass = code;
};
</script>
